package com.yun.algorithmproblem.leetcode;

public class Leetcode63 {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] res = new int[m][n];
        for (int i = m - 1; i >= 0; i--) {
            if (obstacleGrid[i][n - 1] != 0) {
                break;
            }
            res[i][n - 1] = 1;
        }
        for (int i = n - 1; i >= 0; i--) {
            if (obstacleGrid[m - 1][i] != 0) {
                break;
            }
            res[m - 1][i] = 1;
        }
        for (int i = m - 2; i >= 0; i--) {
            for (int j = n - 2; j >= 0; j--) {
                if (obstacleGrid[i][j] != 0) {
                    continue;
                }
                res[i][j] = res[i + 1][j] + res[i][j + 1];
            }
        }
        return res[0][0];
    }
}
